package main.test.SparkAPI.Baics.conditionloop

object testCondiLoop {
  /**
   * control of execution of your code using an if-else construct
   * Iterate through instructions using while and for loops
   * In lesson 4, you learned how to define values and variables
   * to store computation results.
   * But lif is not always so linear;
   * When performing a task, you may also need to make informed decisions by selecting one
   * apporach rather the other.In this lesson,
   * discover how to combine different executions of your
   * code through given conditions.
   * Learn how to use the most common coditional constructs and loops in
   * scala.
   * Use if-else constructs to check if the system should allow a requested
   * operation in the capstone.
   * In the previous lesson, you wrote a program to mark exams from1 to 10.
   * Suppose you need to label a mark as follows:"failed" if the socre is lower than 4,
   * "satisfactory" if between 4 and 6 exclude, "good" if between 6 and
   * 9 exclude, "excellent" otherwise.How would you implement this new functionality?
   * @param args
   */


    //if-else construct

  /**
   * Suppose you have a list of events; each one has a number representing the
   * day of the week it occurs, starting with 1 for Sunday and ending with 7
   * for Saturday. you want to write a function that takes an integer representing a day
   * of the week and returns either weekday or weekend accordingly.
   *
   */
  def categorizeDayOfWeek(n: Int)={
    if(n==1 || n==7) {
      "weekend"
    } else if (n > 1 && n < 7) {
      "weekday"
    } else {
      "unknown"
    }
  }

  /**
   * Another example of using the if-else construct with a function that
   * returns a string to label an integer as positive, negative, or neutral.
   *
   */
  def label(n: Int) =
    if (n == 0) "neutral"
    else if (n < 0) "negative"
    else "positive"


  /**
   * the structure of if-else is similar to other language
   * keyword if is the only one that is mandatory,
   * and it identifies the first condition. You can add more predicates
   * by using keywords else if.
   * Finally, the keyword else defines the behavior for the values that
   * don't satisfy any previous criteria.
   *{{{
   *   if (condition) expr
   *   else if (condition) expr
   *   else expr
   *}}}
   */

  /**
   * expr and curl braces: Omit or not to omit?
   * Expr are everywhere in scala
   * they are in values, variables, conditional
   * constructs, functions, and more!
   * Scala use curly braces to identify expr;
   * one or more instructions make an expr.
   * When an expr contains only one instruction, you can omit
   * its curly braces.For example, the expressions val a= 42 and val a = {42}
   * are equivalent because they both assign the expression 42 to the value a.
   * Let's look at follow codes.You could implement label function as follows:
   * {{{
   *   def label(n:Int) ={
   *   if (n==0) {"neutral"}
   *   else if (n<0) {"negative"}
   *   else {"positive"}
   *
   *   }
   * }}}
   * you can omit the curly braces for the expr associated to each
   * if-condition because they consist of a single expression:
   *
   *
   * You can eliminate the curly braces at the function level: the
   * if-else construct is a single expression the identifies the body of
   * the function label.
   * you should omit curly braces where possible but without compromising the readablility of
   * your code.
   *
   */
  def evenOrOdd(n:Int) =
    if (n % 2 == 0) println(s"$n is even number!")
    else println(s"$n is odd number!")

  //While loop

  /**
   * Image you'd like to write a function called echo5 to print a
   * given message on the console five times.
   * you could write a function that has five print instructions:
   * def echo(msg:String) = {
   * primsg
   * primsg
   * primsg
   * primsg
   * primsg}
   * It doesn't scale: what if you need to print 50
   * messages rather than just5.
   * When you need to repeat an instructon until a particualr condition is
   * satisfied, you
   * can use while loop.
   *
   */
    def echo5(msg: String): Unit = {
      var i = 1
      while (i <= 5) {
        println(msg)
        i += 1
      }
    }

  /**
   * Syntax diagram of while:
   * you repeat its instructions until condition is no longer valid.
   * while (condition) expr
   *
   */

  /**
   * Think in Scala: while loops as antipatterns
   * while loops are part of the scala language, but they are rarely use and
   * often considered antipatterns.
   * While loops operate on mutable state: they keep track of how it evolves,
   * and they stop once a predicate is respected. Scala discourages the use
   * of mutability so while loops are relatively rare in the code.
   * In nunit 5, you'll learn about how the standard Scala collections provides
   * powerful alternavies to while loop through dedicated function such
   * as foreach and map, to iterate in a funcional and thread-safe way.
   *
   */

    def count(n:Int): Unit = {
      var cnt = 1
      while (cnt <= n){
        println(cnt)
        cnt += 1
      }
    }
  // For loop

  /**
   * you saw how to implement echo5 to print a given message five times
   * using a while loop.
   * In scala, you can use for loop to achieve the same result.
   *
   */
  def forEcho5(msg: String) = {
    for (_ <- 1 to 5){
      println(msg)
    }

    /**
     * expr "1 to 5" returns an iterable data structure called
     * Range containing the number 1,2 ,3,4,5(right end included)
     * You can also use the expr "1 until 6" to generate a range with the right end exclude.
     * when using for loop, you repeat an operation over a finite set
     * of values rather than using
     * a stop condition.
     * for (name <- iterable)expr
     * iterable is a iterable structure such as string, range, or list
     * name used to bind the value extracted from the iterable. It
     * is accessible from the expression.
     * expr is blocks of instructions to perform an operation.
     * Scala also offers a more powerful alternative to the classic of loop
     * you have just seen, called for-comprehension. You'll learn about it
     * when disucssing the class Option in unit4.
     *
     *
     *
     */
    /**
     * Summary
     * my objective was to teach about the basic conditioanl construts in scala.
     * you learned how to use if-else construct to express decision making in your
     * code
     * You learned how to use while and for loops to repeat operations multiple times.
     */



  }





  def main(args: Array[String]): Unit = {
    evenOrOdd(7)
    evenOrOdd(88)
    echo5("hello")
    count(10)
    for (a <- "hello") println(a)

  }
}
